// pages/apply/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: [],
		status: 2,
		page: 1,
		pageSize: 20,
		nav: 1,
		role: 1
	},
	changeNav(e) {
		if (this.data.nav == e.currentTarget.dataset.nav) {
			return false;
		}
		this.setData({
			nav: e.currentTarget.dataset.nav,
			page: 1,
		})
		if (e.currentTarget.dataset.nav == 1) {
			this.setData({
				status: 2
			})
			this.onGetList();
		} else {
			this.setData({
				status: 7
			})
			this.onGetList2();
		}
	},
	onApply() {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/isAllowApply',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					wx.navigateTo({
						url: '/pages/apply/opera/index'
					})
				} else {
					wx.showModal({
						title: "提示",
						content: '申请次数上限',
						icon: 'none',
						showCancel: false,
						confirmColor: '#DC5640',
						success(res) {

						}
					})
				}

			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	refresher() {
		if (this.data.page * this.data.pageSize >= this.data.data.total) {
			return false;
		}
		this.setData({
			page: ++this.data.page
		})
		this.onGetList()
	},
	onChangeList(e) {
		let type = e.currentTarget.dataset.type;
		if (type == this.data.status) {
			return false
		}
		this.setData({
			status: type,
			page: 1
		})
		if (this.data.nav == 1) {
			this.onGetList()
		} else {
			this.onGetList2()
		}

	},
	onGetList() {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		let that = this;
		wx.request({
			url: app.globalData.baseUrl + '/api/myOrder/selfRepairIndex',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				page: that.data.page,
				'page-size': that.data.pageSize,
				status: that.data.status
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					that.setData({
						data: res.data.data,
					})
				}

			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	onGetList2() {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		let that = this;
		wx.request({
			url: app.globalData.baseUrl + '/api/myOrder/appointRepairFactoryIndex',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				page: that.data.page,
				'page-size': that.data.pageSize,
				status: that.data.status,
				type: app.globalData.role
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					that.setData({
						data: res.data.data,
					})
				}

			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

		if (app.globalData.role == 2) {
			console.log(3)
			this.setData({
				nav: 2,
				role: 2,
				status:7
			})
		}

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

		console.log(app.globalData.role)
		if (this.data.nav == 1) {
			this.onGetList()
		} else {
			this.onGetList2()
		}

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
